<%--
  Created by IntelliJ IDEA.
  User: ZhuChunXiao
  Date: 2017/5/18
  Time: 13:57
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
  String _id=request.getParameter("_");
%>
<div class="row">
  <input type="hidden" id="departmentId" value="${departmentId}">
  <div class="col-md-12">
    <ul id="tree_post" class="ztree"></ul>
  </div>
</div>

<script>

  var treeObj;

  $(function(){

    init_tree_post();

  });

  function init_tree_post() {

    var setting = {
      async: {
        enable: true,
        url:'<%=path%>/com/personnelInfo/tree?departmentId='+$("#departmentId").val(),
        autoParam: ['departmentId'],
        type: 'get',
        dataType: 'json'
      },
      callback: {
        onAsyncSuccess: zTreeOnAsyncSuccess,
        onCheck: zTreeOnCheck
      },
      check: {
        enable: true,
        <c:if test="${multi=='y'}">
        chkStyle: 'checkbox',
        </c:if>
        <c:if test="${multi=='n'}">
        chkStyle: 'radio',
        radioType: "all",
        </c:if>
        chkboxType:  { 'Y' : '', 'N' : '' }
      },
      data: {
        simpleData: {
          enable: true,
          idKey: 'id',
          pIdKey: 'pId',
          rootPId: ''
        }
      },
      view: {
        autoCancelSelected: false,
        dblClickExpand: false,
        selectedMulti: true
      }
    };

    treeObj = $.fn.zTree.init($('#tree_post'), setting);

  }

  function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) {
//    var zTree = $.fn.zTree.getZTreeObj('tree_post');
//    if (treeNode) {
//      if (treeNode.id === '0') {
//        var nodes = treeNode.children;
//        zTree.selectNode(nodes[0]);
//      }
//    } else {
//      var rootNode = zTree.getNodes()[0];
//      if (rootNode.isParent) {
//        zTree.expandNode(rootNode, true, true, true);
//      }
//    }
  }

  function zTreeOnCheck(event, treeId, treeNode){
//        console.log(treeNode.id + ", " + treeNode.name + "," + treeNode.checked);
  }

  function fn_dialog_posts_select(callback) {
//        var treeObj = $.fn.zTree.getZTreeObj('tree_post');
    var nodes = treeObj.getCheckedNodes();
    console.debug(nodes);
    if(nodes.length < 1){
      __toastr_warning('请至少选择一个岗位');
      callback();
      return;
    }
    var postNames = "";
    var postIds = "";
    for(var i=0; i<nodes.length; i++){
      postNames += nodes[i].name + ",";
      postIds += nodes[i].id + ",";
    }
    postNames = postNames.substring(0,postNames.length-1);
    postIds = postIds.substring(0,postIds.length-1);
    callback({"names":postNames,"ids":postIds});
  }
</script>